package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.bo.AdminGoodsCreateBo;
import com.cskaoyan.bean.bo.AdminGoodsUpdateBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminGoodsCatAndBrandVo;
import com.cskaoyan.bean.vo.AdminGoodsDetailVo;
import com.cskaoyan.bean.vo.AdminGoodsListVo;
import com.cskaoyan.bean.vo.goods.brand.BrandListBean;
import com.cskaoyan.bean.vo.goods.brand.CategoryListBean;
import com.cskaoyan.bean.vo.goods.brand.DataBean;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 商品管理中的商品管理模块
 * @author: Tao
 * @time: 2022/12/28 22:01
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;


    /**
     * @param info
     * @param goodsId
     * @param goodsSn
     * @param name
     * @return 商品列表
     */
    @GetMapping("list")
    public AdminGoodsListVo list(BasePageInfo info, String goodsId, String goodsSn, String name) {
        //过滤掉参数值不对的情况
        Integer id = null;
        if (!StringUtil.isEmpty(goodsId)) {
            try {
                id = Integer.valueOf(goodsId);
            } catch (Exception e) {
                return AdminGoodsListVo.err("参数值不对");
            }
        }
        //参数值都对才会到这里，此时id仍然可能为null
        CommonData<MarketGoods> data = goodsService.getList(info, id, goodsSn, name);
        return AdminGoodsListVo.ok(data,"成功");
    }


    /**
     * @return 回显：返回所有的品牌和种类
     */
    @GetMapping("catAndBrand")
    public AdminGoodsCatAndBrandVo catAndBrand() {
        //存放响应的主要信息：二个list
        DataBean dataBean = new DataBean();
        //获取所有的品牌集合
        List<BrandListBean> brandList = goodsService.getBrand();
        //获取所有的种类集合
        List<CategoryListBean> categoryList = goodsService.getcategory();
        //封装
        dataBean.setBrandList(brandList);
        dataBean.setCategoryList(categoryList);
        return AdminGoodsCatAndBrandVo.ok(dataBean);
    }

    /**
     * 删除商品
     *
     * @param marketGoods
     * @return Vo：{"errno":0,"errmsg":"成功"}
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketGoods marketGoods) {
        // 调用service 进行删除操作
        goodsService.deleteGoods(marketGoods);
        // 响应
        // {"errno":0,"errmsg":"成功"}
        return BaseRespVo.ok("成功");
    }

    /**
     * 完成商品的细节查询
     *
     * @param id
     * @return 商品细节
     */
    @GetMapping("detail")
    public BaseRespVo detail(Integer id) {
        AdminGoodsDetailVo goodsDetail = goodsService.getDetail(id);
        return BaseRespVo.ok(goodsDetail, "成功");
    }

    /**
     * 修改商品
     *
     * @param goodsUpdateBO
     * @return 成功信息
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody AdminGoodsUpdateBo goodsUpdateBO) {
        //修改
        goodsService.updateGoods(goodsUpdateBO.getGoods(), goodsUpdateBO.getAttributes(),
                goodsUpdateBO.getProducts(), goodsUpdateBO.getSpecifications());
        //返回信息
        return BaseRespVo.ok();
    }

    /**
     * 创建商品
     *
     * @param goodsCreateBO
     * @return 成功信息
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody AdminGoodsCreateBo goodsCreateBO) {
        //创建
        //注：返回值为0代表成功，返回值为1代表参数异常
        int state = goodsService.createGoods(goodsCreateBO);
        //返回信息
        if (state == 0) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.errMsgAndNo(402, "参数异常");
        }
    }
}
